package com.cindata

import grails.transaction.Transactional

@Transactional
class AbnormalMortgageBatchFlowStageRoleService
{

    def initFlowStageRole(AbnormalMortgageBatch abnormalMortgageBatch, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.roles?.each {
                def abnormalMortgageBatchFlowStageRole = new AbnormalMortgageBatchFlowStageRole()
                abnormalMortgageBatchFlowStageRole.role = it?.role
                abnormalMortgageBatchFlowStageRole.teamRole = it?.teamRole
                abnormalMortgageBatchFlowStageRole.flowStage = AbnormalMortgageBatchFlowStage.findByBatchAndStage(abnormalMortgageBatch, workflowStage?.stage)
                if (abnormalMortgageBatchFlowStageRole.validate())
                {
                    abnormalMortgageBatchFlowStageRole.save flush: true
                }
                else
                {
                    println abnormalMortgageBatchFlowStageRole.errors
                }
            }
        }
    }
}
